<?php

$conn = mysql_connect("localhost", "root", "yubaokeji");
mysql_select_db("analysis", $conn);

$result = mysql_query("select * from ana_deallogs where dll_status=1", $conn);
$dealrocord = mysql_num_rows($result);

if ($dealrocord > 0) {
    while ($deal = mysql_fetch_array($result)) {
        //检查查询历史
        $temp = mysql_query("select cou_disid from ana_couponhis where cou_orders='" . $deal["dll_ordsn"] . "'");
        $info = mysql_fetch_array($temp);
        //查找对应优惠编码
        $dis = mysql_query("select * from ana_disisbn where dis_id='" . $info["cou_disid"] . "'");
        $type = mysql_fetch_array($dis);
        //优惠代码正常则执行提成
        if ($type["dis_status"] == 1) {
            //生成执行金额SQL
            $class = new Calculation();
            $child = $class->crcNewCheck($deal["dll_ordsn"], $class->getType($deal["dll_ordsn"], $type), $type["dis_id"]);
            //判断是否已存在金额且状态正常
            $histemp = mysql_query("select * from ana_cash where c_ordsn='" . $deal["dll_ordsn"] . "'");
            $his = mysql_fetch_array($histemp);
            if ($his == null) {
                mysql_query($child);
                $parent = mysql_query("select ssa_agid from ana_salestaff where ssa_id='" . $type["dis_ssaid"] . "'");
                $par = mysql_fetch_array($parent);
                if ($par['ssa_agid'] != 0) {
                    $class->amxParentDeal($par['ssa_agid'], $deal["dll_ordsn"]);
                }
            } else {
                mysql_query("update ana_cash set c_status=1 where c_ordsn='" . $deal["dll_ordsn"] . "'");
            }
        }
        mysql_query("update ana_deallogs set dll_status=0 where dll_id=" . $deal["dll_id"]);
    }
}
mysql_close($conn);

class Calculation
{
    private $totalInThisOrder;

    public function getType($osn, $val)
    {
        $sn = $this->getOrderSnList($val["dis_id"], $osn);
        $num = $this->getGoodsNum($osn, $sn);
        $this->totalInThisOrder = $num["now"];
        switch ($val["dis_custype"]) {
            case '21': return $this->amxPartMoney($num["all"], $num["now"]);
                break;
            case '23': return $this->amxLebelMoney($num["all"], $num["now"]);
                break;
            default: return 0;
        }
    }

    /**
     * 查询出用户已推广的订单编号
     */
    private function getOrderSnList($dis, $sn)
    {
        //step-1
        $step_1 = mysql_query("select * from ana_couponhis where cou_orders!='" . $sn . "' and cou_disid=" . $dis);
        $temp_ors = array();
        while ($temp_1 = mysql_fetch_array($step_1)) {
            $temp_ors[] = $temp_1['cou_orders'];
        }
        //step-2
        $sn_in = implode("','", $temp_ors);
        $step_2 = mysql_query("select * from ana_deallogs where dll_ordsn in ('" . $sn_in . "') and dll_status=1");
        $ors = array();
        while ($temp_2 = mysql_fetch_array($step_2)) {
            $ors[] = $temp_2['dll_ordsn'];
        }
        return implode("','", $ors);
    }

    /**
     * 统计已生效的订单数量
     */
    private function getGoodsNum($val, $sn)
    {
        mysql_query("use yyb");
        $list = mysql_query("select * from yub_orderblg where oeg_ordsn in('" . $sn . "') and oeg_sn=1");
        $result["all"] = 0;
        while ($num = mysql_fetch_array($list)) {
            $result["all"] += $num['oeg_num'];
        }
        $getnow = mysql_query("select * from yub_orderblg where oeg_ordsn ='" . $val . "' and oeg_sn=1");
        $result["now"] = 0;
        while ($ou = mysql_fetch_array($getnow)) {
            $result["now"] += $ou['oeg_num'];
        }
        mysql_query("use analysis");
        return $result;
    }

    /**
     * 计算21类型分成
     */
    private function amxPartMoney($all, $now)
    {
        $total = $all + $now;
        if ($total > 10) {
            if ($all > 10) {
                return ($total - $all) * 300;
            } else {
                $exs = (10 - $all);
                return $exs * 200 + ($now - $exs) * 300;
            }
        }
        return $now * 200;
    }

    /**
     * 计算23类型分成
     */
    private function amxLebelMoney($all, $now)
    {
        $total = $all + $now;
        if ($total > 100) {
            if ($all > 100) {
                return ($total - $all) * 400;
            }
            if ($all > 50) {
                return ($total - 100) * 400 + (100 - $all) * 350;
            }
            if ($all <= 50) {
                $temp_01 = ($total - 100) * 400;
                $temp_02 = 50 * 350;
                $temp_03 = (50 - $all) * 300;
                return $temp_01 + $temp_02 + $temp_03;
            }
        }
        if ($total <= 100 && $total > 50) {
            $temp = (50 - $all);
            return $all > 50 ? (100 - $all) * 350 : $temp * 300 + ($now - $temp) * 350;
        }
        return ($total - $all) * 300;
    }

    /**
     * 计算23上级类型分成
     */
    private function amxParLebelMoney($type)
    {
        switch ($type) {
            case '2301': $result = 30;
                break;
            case '2302': $result = 100;
                break;
            default: $result = 0;
        }
        return $result * $this->totalInThisOrder;
    }

    /**
     * 创建sql语句
     */
    public function crcNewCheck($sn, $in, $dis, $agt = null)
    {
        $sql = "insert into ana_cash(c_ordsn,c_in,c_disid,c_agent,c_month,c_status,c_time) value  ('" . $sn . "','";
        $sql.=$in . "','" . $dis . "','" . $agt . "','" . date('Ym') . "','1','" . time() . "')";
        return $sql;
    }

    /**
     * 处理上级分类分成
     */
    public function amxParentDeal($id, $sn)
    {
        $query = mysql_query("select * from ana_saleagent where sag_id=" . $id);
        $info = mysql_fetch_array($query);
        $par = $this->crcNewCheck($sn, $this->amxParLebelMoney($info["sag_type"]), null, $id);
        mysql_query($par);
    }
}
